﻿//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.Data.SqlClient;
//using System.Linq;
//using System.Text;
//using System.Security.Cryptography;

//namespace Quizzer.Model
//{
    //public static class Accounts
    //{
    //    // Change connection string here

    //    static string CurrentDirectory = System.Environment.CurrentDirectory;
    //    static string ConnectionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename="+ CurrentDirectory + "\\db.mdf;Integrated Security=True;Connect Timeout=30";

    //    /// <summary> Register new user. Return true if registration successfull, return false if don't. </summary>
    //    public static bool RegisterAccount(string username, string password)
    //    {
    //        try
    //        {
    //            string passwordMD5 = GetMD5Hash(password);

    //            SqlConnection sqlCon = new SqlConnection(ConnectionString);
    //            sqlCon.Open();

    //            SqlCommand sqlCmdInsert = new SqlCommand("Insert into Users (Login, Password) values ('" + username + "','" + passwordMD5 + "')", sqlCon);
    //            SqlCommand sqlCmdSelect = new SqlCommand("Select * from Users where Login='" + username + "'", sqlCon);

    //            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmdSelect);
    //            DataSet DS = new DataSet();
    //            sqlDA.Fill(DS);

    //            if (DS.Tables[0].Rows.Count > 0)
    //            {
    //                sqlCon.Close();
    //                return false;
    //            }
    //            else
    //            {
    //                sqlCmdInsert.ExecuteNonQuery();
    //                sqlCon.Close();
    //                return true;
    //            }
    //        }
    //        catch { return false; }
    //    }

    //    /// <summary> Delete existing account. Returns true if deleted, return false if don't. </summary>
    //    public static bool DeleteAccount(string username, string password)
    //    {
    //        string passwordMD5 = GetMD5Hash(password);

    //        try
    //        {
    //            SqlConnection sqlCon = new SqlConnection(ConnectionString);
    //            sqlCon.Open();


    //            SqlCommand sqlCmdSelect = new SqlCommand("Select * From Users Where Login='" + username + "' And Password='" + passwordMD5 + "'", sqlCon);
    //            SqlCommand sqlCmdDelete = new SqlCommand("Delete From Users Where Login='" + username + "' And Password='" + passwordMD5 + "'", sqlCon);
    //            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmdSelect);
    //            DataSet DS = new DataSet();
    //            sqlDA.Fill(DS);

    //            if (DS.Tables[0].Rows.Count > 0)
    //            {
    //                sqlCmdDelete.ExecuteNonQuery();
    //                sqlCon.Close();
    //                return true;
    //            }
    //            else
    //            {
    //                sqlCon.Close();
    //                return false;
    //            }
    //        }
    //        catch { return false; }
    //    }

    //    /// <summary>Change username or password.Returns true if modification successfull, return false if don't. </summary>
    //    /// <param name="username"> Old username </param>
    //    /// <param name="password"> Old password </param>
    //    /// <param name="newUsername"> New username </param>
    //    /// <param name="newPassword"> New password </param>
    //    public static bool ModifyAccount(string username, string password, string newUsername, string newPassword)
    //    {
    //        string passwordMD5 = GetMD5Hash(password);
    //        string newPasswordMD5 = GetMD5Hash(newPassword);

    //        try
    //        {
    //            SqlConnection sqlCon = new SqlConnection(ConnectionString);
    //            sqlCon.Open();

    //            SqlCommand sqlCmdSelect = new SqlCommand("Select * From Users Where Login='" + username + "' And Password='" + passwordMD5 + "'", sqlCon);
    //            SqlCommand sqlCmdUpdate = new SqlCommand("Update Users Set Login='" + newUsername + "',Password='" + newPasswordMD5 + "' Where Login='" + username + "' And Password='" + passwordMD5 + "'", sqlCon);
    //            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmdSelect);
    //            DataSet DS = new DataSet();
    //            sqlDA.Fill(DS);

    //            if (DS.Tables[0].Rows.Count > 0)
    //            {
    //                sqlCmdUpdate.ExecuteNonQuery();
    //                sqlCon.Close();
    //                return true;
    //            }
    //            else
    //            {
    //                sqlCon.Close();
    //                return false;
    //            }
    //        }
    //        catch { return false; }
    //    }

    //    /// <summary> Check that user exists. If user exists and login and password right, then return true, else return false. </summary>
    //    public static bool CheckUser(string username, string password)
    //    {
    //        try
    //        {
    //            string passwordMD5 = GetMD5Hash(password);

    //            SqlConnection sqlCon = new SqlConnection(ConnectionString);
    //            sqlCon.Open();

    //            SqlCommand sqlCmd = new SqlCommand("Select * From Users Where Login='" + username + "' and Password='" + passwordMD5 + "'",sqlCon);
    //            SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCmd);
    //            DataSet DS = new DataSet();
    //            sqlDA.Fill(DS);

    //            if (DS.Tables[0].Rows.Count > 0)
    //                return true;
    //            else
    //                return false;
    //        }
    //        catch { return false; }
    //    }

    //    // return md5 hash of the string (for passwords).
    //    private static string GetMD5Hash(string input)
    //    {
    //        MD5 md5Hasher = MD5.Create();
    //        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
    //        StringBuilder sBuilder = new StringBuilder();

    //        for (int i = 0; i < data.Length; i++)
    //            sBuilder.Append(data[i].ToString("x2"));

    //        return sBuilder.ToString();

    //    }
    //}
//}
